package com.kkb.controller;

import com.kkb.vo.Team;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Controller
@RequestMapping("result")
public class ResultController {
//1 ModelAndView
    @RequestMapping("test01")
    public ModelAndView test01(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("teamName","热火队");
        modelAndView.setViewName("result");
        return modelAndView;
    }
//2 返回值是字符串：返回值只能指代要跳转的路径
    @RequestMapping("test02")
    public String test02(HttpServletRequest request){
        Team team = new Team();
        team.setId(11871);
        team.setName("马刺");
        team.setCreateDate(new Date());
        team.setLocation("圣安东尼奥");
        request.getSession().setAttribute("team",team);
        return "result"; //视图解析器把result------>/jsp/result.jsp
    }
//3 反回值是对象，可以返回值自定义的对象，也可以返回各种集合....
    @RequestMapping("test03-1")
    @ResponseBody
    public Integer test031(){
        return 47;
    }
    @RequestMapping("test03-2")
    @ResponseBody
    public String test032(){
        System.out.println("aaa");
        return "666";
    }
    @RequestMapping("test03-3")
    @ResponseBody
    public Team test033(){
        Team team = new Team();
        team.setId(11871);
        team.setName("马刺");
        team.setCreateDate(new Date());
        team.setLocation("圣安东尼奥");
        return team;
    }
    @RequestMapping("test03-4")
    @ResponseBody
    public List<Team> test034(){
        List<Team> list = new ArrayList<Team>();
        for (int i=1;i<=10;i++){
            Team team = new Team();
            team.setId(i);
            team.setName("湖人队");
            team.setLocation("洛杉矶");
            team.setCreateDate(new Date());
            list.add(team);
        }
        return list;
    }
    @RequestMapping("test03-5")
    @ResponseBody
    public Map<String,Team> test035(){
        Map<String,Team> map = new HashMap<>();
        for(int i=1;i<=10;i++){
            Team team = new Team();
            team.setId(i);
            team.setName(i+"太阳队");
            team.setLocation("菲尼克斯");
            team.setCreateDate(new Date());
            map.put(i+"",team);
        }
        return map;
    }
// 4 没有返回值(void) ,使用servlet中的api进行页面跳转
    @RequestMapping("test04-1")
    public void test041(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/jsp/ok.jsp").forward(request,response);
    }
    @RequestMapping("test04-2")
    public void test042(HttpServletResponse response) throws ServletException, IOException{
        response.sendRedirect("/jsp/ok.jsp");
    }
}





